package com.example.springboot_mybatis.controller;

import com.example.springboot_mybatis.MybatisUtils;

import com.example.springboot_mybatis.bean.Score;
import com.example.springboot_mybatis.mapper.ScoreMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("/score")
public class ScoreController {

    @RequestMapping("/list")
    public List<Score> score_findall(){
        SqlSession session = MybatisUtils.getSession();
        ScoreMapper mapper = session.getMapper(ScoreMapper.class);
        List<Score> stus = mapper.selectScore();
        session.close();
        return stus ;
    }

    @RequestMapping("/find")
    public Score score_find(String id1,String id2){
        SqlSession session = MybatisUtils.getSession();
        ScoreMapper mapper = session.getMapper(ScoreMapper.class);
        Score stu = mapper.selectScoreOne(id1,id2);
        session.close();
        return stu ;
    }

    @RequestMapping("/delete")
    public void score_delete( String id1,String id2){
        SqlSession session = MybatisUtils.getSession();
        ScoreMapper mapper = session.getMapper(ScoreMapper.class);
        mapper.deleteScore(id1,id2);
        session.commit();
        session.close();
    }

    @RequestMapping("/add")
    public void score_add(String id1,String id2,String score){
        SqlSession session = MybatisUtils.getSession();
        ScoreMapper mapper = session.getMapper(ScoreMapper.class);
        mapper.addScore(id1,id2,score);
        session.commit();
        session.close();
    }

    @RequestMapping("/edit")
    public void score_edit(String id1,String id2,String score){
        SqlSession session = MybatisUtils.getSession();
        ScoreMapper mapper = session.getMapper(ScoreMapper.class);
        mapper.editScore(id1,id2,score);
        session.commit();
        session.close();
    }
}
